<?php
namespace Home\Controller;
use Think\Controller;
/**
 * 活动控制器
 */
class ActivityController extends  HomeController{
  
    /**
     * 活动列表
     */
    public function index()
    {
       $count=M('activity')->where("pid=0")->count();
        $page=I('gyunying_useret.page','1','strip_tags');
        $pagearr= getPageArr($page,C('PAGESIZE'),$count);

       $result=M('activity')->where("pid=0")->field("id,parenttheme,type,state")->limit($pagearr['start'],$pagearr['pagesize'])->select();
       foreach ($result as $key => $value) {
         $result[$key]['child']=M('activity')->where("pid='%d'",$value['id'])->Field("id,childtheme,opentime,endtime")->select();
       }
       
       foreach($result as $k=>$v)
       {
         if($v['type']==0) {
          $result[$k]['type']='';

         }
         if($v['type']==2)
         {
            $result[$k]['type']='单一设备';
         }
         if($v['type']==3)
         {
           $result[$k]['type']='经销商';
         }
       
       }
      
       $this->assign("activity",$result);
       $this->assign("pagearr",$pagearr);
       $this->display();
    }

    /**
     * 添加活动
     */
    public function append(){
    if($_POST){
        $activity=M('activity');
        $activity->parenttheme=I('post.parenttheme','','strip_tags');
         $activity->addtime=time();
         $activity->type=I('post.type',0,'strip_tags');
         $config['maxsize']=C('IMG_MAX_SIZE');
         $config['path']=C('Admin_IMG_PATH');
         $img=imgUpload($config);
         $parentimg=$img['parentimg'];
         
           if(!empty($parentimg['savename']))
        {
            $activity->parentimg=substr(C('Admin_IMG_PATH'),1).$parentimg['savepath'].$parentimg['savename'];
        }
         $data['pid']=$activity->add();


        ////添加子活动
        $childimg=$img['childimg'];
        if(!empty($childimg['savename'])){
              $data['childimg']=substr(C('Admin_IMG_PATH'),1).$childimg['savepath'].$childimg['savename'];
               

        }
        $data['addtime']=time();
        $data['opentime']=strtotime(I("post.opentime",0,"strip_tags"));
        $data['endtime']=strtotime(I("post.endtime",0,"strip_tags"));
        $data['childtheme']=I('post.childtheme','','strip_tags');
        $cid=$activity->add($data);
       if($cid>0&&$data['pid']>0)
        {
            echo "<script>alert('活动主题添加成功');window.location.href='./index'</script>";
        }
        

    }else{

        $this->display();
    }
    }

   
    /**
     * 编辑父活动静态页
     */
public function parentedit(){
  if($_POST)
  {
    $where['id']=I('post.id',0,'strip_tags');
    $data['parenttheme']=I('post.parenttheme','','strip_tags');
    if(!empty($_FILES['parentimg']['tmp_name'])){
       $config['maxsize']=C('IMG_MAX_SIZE');
         $config['path']=C('Admin_IMG_PATH');
         $img=imgUpload($config);
         $parentimg=$img['parentimg'];

          $data['parentimg']=substr(C('Admin_IMG_PATH'),1).$parentimg['savepath'].$parentimg['savename'];


    }
    $id=M('activity')->where($where)->save($data);

    if($id!==false){
      echo "<script>alert('父主题修改成功');window.location='index'</script>";

    }
  }else
  {
  $where['id']=I('get.id',0,'strip_tags');
  $result=M('activity')->where($where)->getField("id,parenttheme,type,parentimg");
 
  $this->assign('res',array_values($result));
  $this->display();
  }


} 

   /**
    *编辑子主题 
    */ 
    public function childedit(){
  if($_POST)
  {

    $where['id']=I('post.id',0,'strip_tags');
    $data['parenttheme']=I('post.parenttheme','','strip_tags');
     $data['opentime']=strtotime(I("post.opentime",0,"strip_tags"));
    $data['endtime']=strtotime(I("post.endtime",0,"strip_tags"));
    if(!empty($_FILES['childimg']['tmp_name'])){
       $config['maxsize']=C('IMG_MAX_SIZE');
         $config['path']=C('Admin_IMG_PATH');
         $img=imgUpload($config);
         $childimg=$img['childimg'];
          $data['childimg']=substr(C('Admin_IMG_PATH'),1).$childimg['savepath'].$childimg['savename'];
    }
    $id=M('activity')->where($where)->save($data);
   

    if($id!==false){
      echo "<script>alert('子主题修改成功');window.location='index'</script>";

    }
  }else
  {
  $where['id']=I('get.id',0,'strip_tags');
  $result=M('activity')->where($where)->getField("id,childtheme,opentime,endtime,childimg");
 
  $this->assign('res',array_values($result));
  $this->display();
  }


} 
/**
   * 删除父主题
   */  
public function parentdel(){

   $activity=M('activity');
   $device_item=M('activ_item');
   $activity->startTrans();
 ///删除顶级strat
  $id=I('get.id',0,'strip_tags');
  $top=$activity->where("id='%d'",$id)->delete();
  ///删除顶级end
  ///获取所的子级strat;
   $where['pid']=I('get.id',0,'strip_tags');
  $cids=M('activity')->where($where)->getField("id,pid");
  $cids=implode(",",array_keys($cids));
  $map['activityid']=array('in',$cids);
  $second=$activity->where($where)->delete();
   ///删除子级end
  /////删除产品
$itemid=M('activ_item')->where("pid='%d'",$id)->getField('id');///判读活动产品是否存在
if($itemid)
{
$item=$device_item->where("pid='%d'",$id)->delete();////删除活动产品
}else{
  $item=1;

}
////删除设备
$de=M('activity_device')->where("activityid='%d'",$id)->getField("id");////判读设备是否存在
if($de){
  $device=M('activity_device')->where("activityid='%d'",$id)->delete();
  }else{
    $device=1;

  }
  ////删除活动设备

  if($item>0 && $device>0){
    $activity->commit();//删除成功
    echo '1';

  }else{
  $activity->rollback();///没有删除成功
  echo '2';
  }

  
  


}
/**
 * 删除子活动
 */
public function childdel(){
  $activity=M('activity');
  $device_item=M('activ_item');
   $where['id']=I('post.id',0,'strip_tags');
   $second=$activity->where($where)->delete();
   $map['cid']=I('post.id',0,'strip_tags');
   ///通过id查找产品
   $id=M('activ_item')->where($map)->getField("id",true);
   if(!empty($id))
   {
     $items=$device_item->where($map)->delete();
   }else{
    $items=1;
   }

   
   $activity->startTrans();
   if($second && $items){
    $activity->commit();
   echo '1';
   }else{
    $activity->rollback();
    echo '2';
    
   }

}


/**
 * 添加子活动
 */
public function childadd(){
  $data['pid']=I('post.pid',0,'strip_tags');
  $data['childtheme']=I('post.childtheme','','strip_tags');
  $data['opentime']=strtotime(I("post.opentime",0,"strip_tags"));
    $data['endtime']=strtotime(I("post.endtime",0,"strip_tags"));
      $config['maxsize']=C('IMG_MAX_SIZE');
         $config['path']=C('Admin_IMG_PATH');
         $img=imgUpload($config);
         $childimg=$img['childimg'];
     if(!empty($childimg['savename']))
     {
         $data['childimg']=substr(C('Admin_IMG_PATH'),1).$childimg['savepath'].$childimg['savename'];
               
        }
        $id=M('activity')->add($data);
        if($id>0){
        $this->success('子活动添加成功');
          
        }


}
/**
 * 设置活动设备
 */
public function device(){
  
  $id=I('get.id',0,'strip_tags');////活动id
  $typeid=M('activity')->where("id='%d'",$id)->getField("type");
  if($typeid==2){
    $type='单一设备';
  }
  else{
  $type='经销商';
  }

  ///$type=I('get.type','','strip_tags');

 $this->assign('type',$type)->assign('typeid',$typeid);
 ///获取所有设备
  $devices=M('device')->getField('devicecode',true);
            $temp_devices=array();
            foreach ($devices  as $key => $value) {
                $temp_devices[$key]['label']=$value;
                $temp_devices[$key]['value']=$value;
            }

 $this->assign('devices',json_encode($temp_devices,JSON_UNESCAPED_UNICODE));
////获取搜有加盟商
 $shops=M('shop_user')->getField("companyname",true);
 $temp_shops=array();
 foreach ($shops as $key => $value) {
   $temp_shops[$key]['label']=$value;
   $temp_shops[$key]['value']=$value;

 
 }

 $this->assign('shops',json_encode($temp_shops,JSON_UNESCAPED_UNICODE));
 $this->assign('id',$id);


 $this->display();
}
/**
 * 关联活动和设备
 */
public function Actdevice(){
 
  $typeid=I('post.typeid',0,'strip_tags');
  ////通过单一设备添加
  if($typeid==2){
    $where['devicecode']=I('post.device','','strip_tags');
    $data['deviceid']=M('device')->where($where)->getField("deviceid");
    $data['activityid']=I('post.id',0,'strip_tags');
    $id=M('activity_device')->add($data);
    if($id>0)
    {
      $this->success('设备添加成功');
    }

  }
  ////通过经销商添加设备
  if($typeid==3)
  {
    $map['a.companyname']=I('post.shop','','strip_tags');
    $devices=M('shop_user a')->join('cart_device b on a.shopid=b.shopid')->where($map)->getField('b.deviceid,a.shopid');
    foreach ($devices as $key => $value)
     {
      $da['deviceid']=$key;
      $da['activityid']=I('post.id',0,'strip_tags');
       $id=M('activity_device')->add($da);
    }

    if($id>0)
    {
       $this->success('设备添加成功');

    }




  }
 
}
/**
 * 检测设备是否存在
 */
public function Checkdevice(){
  $where['devicecode']=I('get.device','','strip_tags');
  $id=M('device')->where($where)->getField("deviceid");
 if(empty($id)){
 $data = array('status'=>404,'code'=>'error','info'=>'该设备不存在');
 echo json_encode($data);
 }else{
 $data = array('status'=>200,'code'=>'success','info'=>'允许添加');
  echo json_encode($data);
 }

}
/**
 * 检测商家是否存在
 */
public function Checkshop(){
   $where['companyname']=I('get.shop','','strip_tags');
  $id=M('shop_user')->where($where)->getField("shopid");
 if(empty($id)){
 $data = array('status'=>404,'code'=>'error','info'=>'不存在的加盟商');
 echo json_encode($data);
 }else{
 $data = array('status'=>200,'code'=>'success','info'=>'允许添加');
  echo json_encode($data);
 }

}
/**
 * 活动上线下线
 */
public function online(){
  $where['id']=I('get.id',0,'strip_tags');
 
  $re=M('activity')->where($where)->setField('state','1');
  $map['pid']=I('get.id',0,'strip_tags');
  $res=M('activity')->where($map)->setField('state','1');

  
  if($re!==false && $res!==false){
    $data = array('status'=>200,'code'=>'success','info'=>'活动已经上线');
        echo json_encode($data);
  }else{
    $data = array('status'=>404,'code'=>'success','info'=>'上线出现故障');
        echo json_encode($data);

  }

}
/**
 * 活动下线
 */
public function downline(){
  $where['id']=I('get.id',0,'strip_tags');
 
  $re=M('activity')->where($where)->setField('state','0');
  $map['pid']=I('get.id',0,'strip_tags');
  $res=M('activity')->where($map)->setField('state','0');
  if($re!==false && $res!==false){
    $data = array('status'=>200,'code'=>'success','info'=>'活动已经下线');
        echo json_encode($data);
  }else{
    $data = array('status'=>404,'code'=>'success','info'=>'下线出现故障');
        echo json_encode($data);

  }

}

/**
 * 参与活动设备列表
 */
public function devicelist()
{

  $arg['page']=I('get.page',1,'strip_tags');
  ////活动主题
  $where['pid']=0;
  $theme=M('activity')->where($where)->getField("id,parenttheme");
  $device=D('Activity');
  $devices=$device->index($arg);
  
  $this->assign('devices',$devices['devices']);
   $this->assign('pagearr',$devices['pagearr']);

  $this->assign('theme',$theme);
   $this->display(); 
}
/**
 * 搜索活动设备
 */
public function Seardevice()
{
  $arg['activityid']=I('post.activityid','','strip_tags');
  $arg['page']=I('post.page',1,'strip_tags');
  $device=D('Activity');
  $result=$device->index($arg);
  if(!empty($result['devices']))
  {
    $result['status'] =1;
    echo json_encode($result,JSON_UNESCAPED_UNICODE);
    return '';
  }
  $result['status']=0;
  $result['message']='暂无数据';
  echo json_encode($result,JSON_UNESCAPED_UNICODE);
}
/**
 * 设备删除
 */
public function deldevice()
{
   $where['id']=I('get.id','','strip_tags');
   $res=M('activity_device')->where($where)->delete();
   if($res)
   {
      $data = array('status'=>200,'code'=>'success','info'=>'设备解除了该活动');
      echo json_encode($data);
   }else
   {
      $data = array('status'=>404,'code'=>'success','info'=>'操作失误');
      echo json_encode($data);
   }

}

}